<template>
  <div class="privilege">
    <!-- 按钮 -->
     <div class="btns">
      <el-button size="small" type="primary" @click="toAdd">添加</el-button>
    </div>
    <!-- 表格 -->
     <el-table :data="list" style="width: 100%" size="small">
      <el-table-column type="index" label="序号"> </el-table-column>
      <el-table-column prop="name" label="姓名" width="180"> </el-table-column>
      <el-table-column prop="icon" label="图标"> </el-table-column>
      <el-table-column type="type" label="风格"> </el-table-column>
      <el-table-column prop="path" label="路径"> </el-table-column>
      <el-table-column prop="is_hidden" label="隐藏"> </el-table-column>
      <el-table-column prop="parent_id" label="上级"> 
        <template v-slot="scope">
          <el-button size="small" type="danger" @click="del(scope.row)">删除</el-button>
          <el-button size="small" type="primary" @click="edit(scope.row)">修改</el-button>
        </template>
       </el-table-column>
    </el-table>
   
    <!-- 模态框 -->
    
    <el-dialog
      class="customer_modal"
      title="添加privilege"
      :visible.sync="dialogVisible"
      width="60%">
            <el-form :model="form" label-width="80px">
            <el-form-item label="序号" >
              <el-input v-model="form.id"></el-input>
            </el-form-item>

            <el-form-item label="姓名" >
             <el-input v-model="form.name"></el-input>
            </el-form-item>

            <el-form-item label="图标" >
             <el-input v-model="form.icon"></el-input>
            </el-form-item>

            <el-form-item label="风格" >
             <el-input v-model="form.type"></el-input>
            </el-form-item>

            <el-form-item label="路径" >
             <el-input v-model="form.path"></el-input>
            </el-form-item> 

            <el-form-item label="隐藏" >
             <el-input v-model="form.is_hidden"></el-input>
            </el-form-item>

            <el-form-item label="上级" >
             <el-input v-model="form.parent_id"></el-input>
            </el-form-item>


          </el-form>
      
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogVisible = false" size="small">取 消</el-button>
        <el-button type="primary" @click="save(form)" size="small">确 定</el-button>
      </span>
    </el-dialog>

  </div>
</template>

<script>
//导入request ,浏览器提供给我们的用于与后台进行交互的工具 api
import {get,post}  from '@/utils/request'
export default {
  data (){
    return {
      form:{},
      list:[],
      dialogVisible:false,
      dialogVisible:false,
      dialogVisible:false
    }
  },
  created(){
    this.loadData();
  },
  methods:{
       save(){
        let url = "/privilege/saveOrUpdate"
        post(url,this.form).then((response)=>{
          //1.模态框关闭
          this.dialogVisible = false;
          //2.提示
          this.$message({type:"success",message:response.message});

          //3.刷新
          this.loadData();
        }
        )
      },
    //删除
    del(row){
        let url = "/privilege/deleteById"
        post(url,{id:row.id}).then((response)=>{
          //1.提示
          this.$message({type:"success",message:response.message});
          //2.刷新
          this.loadData();
        })
      },
    //编辑
    edit(row){
        this.form = row;
        this.dialogVisible = true;
    },
    //加载数据
    loadData(){
      let url = "/privilege/findAll"
      get(url).then( (response)=>{
        //response就是后端返回给我们的Message{status,message,data,timestamp}
        this.list = response.data;
      } );

    },
    toAdd(){
      this.dialogVisible = true;
    },
    data(){
      return{
        list:[],
        dialogVisible:false
      }
    },
    toDelete(){
     this.dialogVisible = true;
    },
    toSelect(){
    this.dialogVisible = true;
    },

  }
  
}
</script>